module.exports = (app) => {
  const { INTEGER, STRING, DATE, TEXT } = app.Sequelize;
  const moment = require("moment");
  // 配置（重要：一定要配置详细，一定要！！！）
  const Role = app.model.define("role", {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: "主键ID"
    },
    rolename: {
      type: STRING(30),
      allowNull: false,
      defaultValue: "",
      unique: true,
      comment: "角色名称"
    },
    desc: {
      type: TEXT,
      allowNull: false,
      defaultValue: "",
      comment: "角色描述"
    },
    creator: {
      type: STRING(30),
      allowNull: false,
      defaultValue: "",
      comment: "创建人"
    },
    created_time: {
      type: DATE,
      get() {
        const v = this.getDataValue("created_time");
        return v ? moment(v).format("YYYY-MM-DD HH:mm:ss") : v;
      }
    },
    updated_time: {
      type: DATE,
      get() {
        const v = this.getDataValue("created_time");
        return v ? moment(v).format("YYYY-MM-DD HH:mm:ss") : v;
      }
    }
  });

  // 关联
  Role.associate = function () {
    app.model.Role.hasMany(app.model.User, {
      foreignKey: "roleId"
    });
  };

  return Role;
};
